package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.commom.PageResult;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiceImpl  implements CourseService {
    @Autowired
    CourseMapper CourseMapper;
    @Override
    public void deleteById(Integer id) {
        CourseMapper.deleteById(id);

    }
    @Override
    public void addCourse(Course course) {

        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        CourseMapper.insert(course);
    }
    @Override
    public Course getById(Integer id) {
        return CourseMapper.getById(id);
    }
    @Override
    public void update(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        CourseMapper.update(course);
    }
    @Override
    public List<Course> listAll() {
        return CourseMapper.listAll();
    }
    @Override
    public List<Course> listBySubject(Integer subject) {
        return CourseMapper.listBySubject(subject);
    }
    @Override
    public PageResult<Course> listCourses(String name, Integer subject, Integer target, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Course> list = CourseMapper.listCourses(name,subject,target);
        Page<Course> coursePage = (Page<Course>) list;
        return new PageResult<>( coursePage.getTotal(), coursePage.getResult());
    }

    }




